package com.oneplatform.organisation.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesuite.common.JeesuiteBaseException;
import com.jeesuite.common.util.BeanUtils;
import com.jeesuite.mybatis.plugin.pagination.PageParams;
import com.jeesuite.security.client.LoginContext;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.exception.ExceptionCode;
import com.oneplatform.base.model.PageQueryParam;
import com.oneplatform.base.model.PageResult;
import com.oneplatform.organisation.dao.entity.EmployeeEntity;
import com.oneplatform.organisation.dto.param.EmployeeParam;
import com.oneplatform.organisation.service.EmployeeService;

import io.swagger.annotations.ApiOperation;

/**
 * generated by www.jeesuite.com
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

private @Autowired EmployeeService employeeService;
	
	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
    public @ResponseBody PageResult<EmployeeEntity> pageQueryEmployees(@ModelAttribute PageQueryParam param) {
		PageResult<EmployeeEntity> page = employeeService.pageQuery(new PageParams(param.getPageNo(), param.getPageSize()), param.getConditions());
		return page;
	}
	
	@ApiOperation(value = "按id查询")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
    public @ResponseBody WrapperResponse<EmployeeEntity> getById(@PathVariable("id") int id) {
		EmployeeEntity entity = employeeService.findEmployeeById(id);
		return new WrapperResponse<>(entity);
	}
	
	@ApiOperation(value = "新增")
	@RequestMapping(value = "add", method = RequestMethod.POST)
    public @ResponseBody WrapperResponse<String> addEmployee(@RequestBody EmployeeParam param) {
		if(param.getDepartmentId() == null || param.getDepartmentId() == 0){
			throw new JeesuiteBaseException(ExceptionCode.REQUEST_PARAM_REQUIRED.code, "请先选择所在部门");
		}
		if(param.getPositionId() == null || param.getPositionId() == 0){
			throw new JeesuiteBaseException(ExceptionCode.REQUEST_PARAM_REQUIRED.code, "请先选择职位");
		}
		EmployeeEntity entity = BeanUtils.copy(param, EmployeeEntity.class);
		entity.setCreatedAt(new Date());
		entity.setCreatedBy(LoginContext.getIntFormatUserId());
		
		employeeService.addEmployee(entity,param.getDepartmentId(),param.getPositionId());
		
		return new WrapperResponse<>();
	}
	
	@ApiOperation(value = "更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody WrapperResponse<String> updateEmployee(@RequestBody EmployeeParam param) {
		EmployeeEntity entity = BeanUtils.copy(param, EmployeeEntity.class);
		entity.setUpdatedAt(new Date());
		entity.setUpdatedBy(LoginContext.getIntFormatUserId());
		employeeService.updateEmployee(entity);
		
		return new WrapperResponse<>();
	}
	
	@ApiOperation(value = "删除")
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
    public @ResponseBody WrapperResponse<String> deleteEmployee(@PathVariable("id") int id) {
		employeeService.deleteEmployee(id);
		return new WrapperResponse<>();
	}
	
	@ApiOperation(value = "离职")
	@RequestMapping(value = "leave/{id}", method = RequestMethod.POST)
    public @ResponseBody WrapperResponse<String> setEmployeeNotInActive(@PathVariable("id") int id) {
		EmployeeEntity entity = new EmployeeEntity();
		entity.setId(id);
		entity.setInActive(false);
		entity.setUpdatedAt(new Date());
		entity.setUpdatedBy(LoginContext.getIntFormatUserId());
		employeeService.updateEmployee(entity);
		return new WrapperResponse<>();
	}
	
}
